<?php

class UserController extends Controller
{

    public $layout = '//layouts/body';
    public $pageTitle = '课堂资源';
    //显示用户首页
    public function actionIndex()
    {
        $openid = Yii::app()->request->getParam('openid');
        $app_info = CityApp::model()->findByPk(1);
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        if($app_info->is_type==1){//web类型
            if($user_id){
                $user_info = People::model()->findByPk($user_id);//获取用户信息
                $status = 'ok';
            }else{//不存在
                $status = 'no';
            }
        }elseif($app_info->is_type==2){//订阅号
            if($user_id){
                $user_info = People::model()->findByPk($user_id);//获取用户信息
                $status = 'ok';
            }else{//不存在
                $user_info = People::model()->find('is_login=:is_login and openid=:openid',array(':is_login'=>1,':openid'=>$openid));//获取用户信息
                if($user_info){
                    $status = 'ok';
                }else{
                    if($openid){
                        $this->redirect($this->createUrl('index/login',array('openid'=>$openid)));
                    }else{
                        $this->redirect($this->createUrl('index/login'));
                    }
                }
            }
        }else{//服务号
            if($user_id){
                $user_info = People::model()->findByPk($user_id);
            }else{
                $openid = XUtils::isBasicAuth($app_info->wx_app_id,$app_info->wx_app_secret);
                $user_info = People::model()->find('is_login=:is_login and openid=:openid',array(':is_login'=>1,':openid'=>$openid));//获取用户信息
                if(!$user_info){
                    $this->redirect($this->createUrl('index/login'));
                }else{
                    Yii::app()->session['user_id'] = $user_info->id;
                }
            }
        }
        $this->pageTitle = '个人中心';
        if($user_info->role==1){//学生

            //积分总数
            $this->render('index',array('openid'=>$openid,'status'=>$status,'user_info'=>$user_info));
        }elseif($user_info->role==2){//家长
            $child_id = $user_info->relation_id;
            $child_info = People::model()->findByPk($child_id);
            $this->render('index_2',array('openid'=>$openid,'status'=>$status,'user_info'=>$user_info,'child_info'=>$child_info));
        }elseif($user_info->role==3){//家长
            $this->render('index_3',array('openid'=>$openid,'status'=>$status,'user_info'=>$user_info));
        }else{
            $this->redirect(Yii::app()->createUrl('index/login',array('openid'=>$openid)));
        }
    }
    
    public function actionMember()
    {
        $openid = Yii::app()->request->getParam('openid');
        $app_info = CityApp::model()->findByPk(1);
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        if($app_info->is_type==1){//web类型
            if($user_id){
                $user_info = People::model()->findByPk($user_id);//获取用户信息
                $status = 'ok';
            }else{//不存在
                $status = 'no';
            }
        }elseif($app_info->is_type==2){//订阅号
            if($user_id){
                $user_info = People::model()->findByPk($user_id);//获取用户信息
                $status = 'ok';
            }else{//不存在
                $user_info = People::model()->find('is_login=:is_login and openid=:openid',array(':is_login'=>1,':openid'=>$openid));//获取用户信息
                if($user_info){
                    $status = 'ok';
                }else{
                    if($openid){
                        $this->redirect($this->createUrl('index/login',array('openid'=>$openid)));
                    }else{
                        $this->redirect($this->createUrl('index/login'));
                    }
                }
            }
        }else{//服务号
            if($user_id){
                $user_info = People::model()->findByPk($user_id);
            }else{
                $openid = XUtils::isBasicAuth($app_info->wx_app_id,$app_info->wx_app_secret);
                $user_info = People::model()->find('is_login=:is_login and openid=:openid',array(':is_login'=>1,':openid'=>$openid));//获取用户信息
                if(!$user_info){
                    $this->redirect($this->createUrl('index/login'));
                }else{
                    Yii::app()->session['user_id'] = $user_info->id;
                }
            }
        }
        $this->pageTitle = '个人中心';
        if($user_info->role==1){//学生
    
            //积分总数
            $this->render('member',array('openid'=>$openid,'status'=>$status,'user_info'=>$user_info));
        }elseif($user_info->role==2){//家长
            $child_id = $user_info->relation_id;
            $child_info = People::model()->findByPk($child_id);
            $this->render('member',array('openid'=>$openid,'status'=>$status,'user_info'=>$user_info,'child_info'=>$child_info));
        }elseif($user_info->role==3){//家长
            $this->render('member',array('openid'=>$openid,'status'=>$status,'user_info'=>$user_info));
        }else{
            $this->redirect(Yii::app()->createUrl('index/login',array('openid'=>$openid)));
        }
    }
    public function actionOpusFileList()
    {
        $this->pageTitle = '作品上传';
        $this->render('opusFileList');
    }
    
    public function actionOpustmodel()
    {
         $pTitle = '课堂资源';
         $connection = Yii::app()->db;
         $oid = Yii::app()->request->getParam('oid');
         $opus_list_sql = "select * from tbl_opus as o where o.opus_from='T' and o.id=$oid";
         $orderby=" order by o.create_time desc";
         $opus_list_sql=$opus_list_sql.$orderby;
         $command = $connection->createCommand($opus_list_sql);
         $list = $command->queryAll();
         $newlist;
         foreach($list as $k=>$v){
             $p_list_sql ="select * from tbl_opus_list as o where o.opus_id=".$v['id'];
             $comment_list__sql ="select p.user_name as user_name,o.content as content,o.comment_from as comment_from,o.create_time as create_time from tbl_opus_comment as o,tbl_people as p where o.opus_id=".$v['id']." and p.id=o.st_id order by o.create_time desc";
             $zan_list__sql ="select p.user_name as user_name from tbl_opus_zan as o,tbl_people as p where o.opus_id=".$v['id']." and p.id=o.st_id order by o.create_time desc";
             $command1 = $connection->createCommand($p_list_sql);
             $command2 = $connection->createCommand($comment_list__sql);
             $command3 = $connection->createCommand($zan_list__sql );
             $list1 = $command1->queryAll();
             $list2 = $command2->queryAll();
             $list3 = $command3->queryAll();
             $newlist[$k]['userinfo'] = People::model()->findByPk($v['st_id']);
             $newlist[$k]['opus'] = $v;
             $newlist[$k]['opuslist'] = $list1;
             $newlist[$k]['commentlist'] = $list2;
             $newlist[$k]['zanlist'] = $list3;
         }
         $this->render('opustmodel',array(
             'ptitle'=>$pTitle,
             'newlist'=>$newlist
         ));
    }
    
    
    
    public function actionOpusTList()
    {
        $connection = Yii::app()->db;
        $pTitle = '课堂资源';
        $opus_list_sql = "select * from tbl_opus as o where o.opus_from='T' ";
        $orderby=" order by o.create_time desc";
        $opus_list_sql=$opus_list_sql.$orderby;
        $command = $connection->createCommand($opus_list_sql);
        $list = $command->queryAll();
        $newlist;
        foreach($list as $k=>$v){
            $p_list_sql ="select * from tbl_opus_list as o where o.opus_id=".$v['id'];
            $comment_list__sql ="select p.user_name as user_name,o.content as content,o.comment_from as comment_from,o.create_time as create_time from tbl_opus_comment as o,tbl_people as p where o.opus_id=".$v['id']." and p.id=o.st_id order by o.create_time desc";
            $zan_list__sql ="select p.user_name as user_name from tbl_opus_zan as o,tbl_people as p where o.opus_id=".$v['id']." and p.id=o.st_id order by o.create_time desc";
            $command1 = $connection->createCommand($p_list_sql);
            $command2 = $connection->createCommand($comment_list__sql);
            $command3 = $connection->createCommand($zan_list__sql );
            $list1 = $command1->queryAll();
            $list2 = $command2->queryAll();
            $list3 = $command3->queryAll();
            $newlist[$k]['userinfo'] = People::model()->findByPk($v['st_id']);
            $newlist[$k]['opus'] = $v;
            $newlist[$k]['opuslist'] = $list1;
            $newlist[$k]['commentlist'] = $list2;
            $newlist[$k]['zanlist'] = $list3;
        }
        $this->render('opusTList',array(
                'ptitle'=>$pTitle,
                'newlist'=>$newlist
            ));
    }
    
    
    public function actionOpuslist()
    {

        //根据类型判断进入的列表页，1：我的，2：其他，3：课堂。
        //默认为其他作品列表页
        $type = Yii::app()->request->getParam('type');
        $gid = Yii::app()->request->getParam('gid');
        $cid = Yii::app()->request->getParam('cid');
        $pname = Yii::app()->request->getParam('pname');
        $orderby = Yii::app()->request->getParam('orderby');
        $user_id = Yii::app()->session['user_id'];
        $user_info = People::model()->findByPk($user_id);//获取用户信息
        $opus_list_sql="";
        if ($type=="1"){
          $pTitle = '我的作品';
            $opus_list_sql = "select o.id,o.opus_title,o.opus_desc,o.opus_type,o.st_id,o.opus_from,o.opus_source_type,o.total_zan,o.create_time from tbl_opus as o where o.st_id=$user_id";
        }elseif ($type=="2" || $type==""){
            $pTitle = '其他作品';
            if($gid!="" && $gid!="-1"){
                $opus_list_sql = "select o.id,o.opus_title,o.opus_desc,o.opus_type,o.st_id,o.opus_from,o.opus_source_type,o.total_zan,o.create_time from tbl_opus as o,tbl_people as p where o.opus_source_type='作品上传' and p.gid=$gid and o.st_id=p.id";
            }else{
                if($cid!="" && $cid!="-1"){
                   $opus_list_sql = "select o.id,o.opus_title,o.opus_desc,o.opus_type,o.st_id,o.opus_from,o.opus_source_type,o.total_zan,o.create_time from tbl_opus as o,tbl_people as p where o.opus_source_type='作品上传' and p.cid=$cid and o.st_id=p.id";
                }else{
                    if($pname!="" && $pname!="undefined"){
                      $opus_list_sql = "select o.id,o.opus_title,o.opus_desc,o.opus_type,o.st_id,o.opus_from,o.opus_source_type,o.total_zan,o.create_time from tbl_opus as o,tbl_people as p where o.opus_source_type=作品上传' and p.user_name='$pname' and o.st_id=p.id";
                    }else{
                        $opus_list_sql = "select o.id,o.opus_title,o.opus_desc,o.opus_type,o.st_id,o.opus_from,o.opus_source_type,o.total_zan,o.create_time from tbl_opus as o where o.opus_source_type='作品上传'";
                    }
                }
            }
           
        }elseif ($type=="3"){
            $pTitle = '课堂资源';
             $opus_list_sql = "select * from tbl_opus as o where o.opus_source_type='课堂资源' ";
        }
        if($orderby==""){
            $orderby=" order by o.create_time desc";
            $opus_list_sql=$opus_list_sql.$orderby;
        }elseif($orderby=="new"){
            $orderby=" order by o.create_time desc";
            $opus_list_sql=$opus_list_sql.$orderby;
        }elseif($orderby=="zan"){
            $orderby=" order by o.total_zan desc";
            $opus_list_sql=$opus_list_sql.$orderby;
        }
        $connection = Yii::app()->db;
        $class_info =  $connection->createCommand("select * from tbl_classes where 1=1");
        $class_info = $class_info->queryAll();
        $grade_info = $connection->createCommand("select * from tbl_grade where 1=1");
        $grade_info = $grade_info->queryAll();
        $command = $connection->createCommand($opus_list_sql);
        $list = $command->queryAll();
        $newlist;
       foreach($list as $k=>$v){
           $p_list_sql ="select * from tbl_opus_list as o where o.opus_id=".$v['id'];
           $comment_list__sql ="select p.user_name as user_name,o.content as content,o.comment_from as comment_from,o.create_time as create_time from tbl_opus_comment as o,tbl_people as p where o.opus_id=".$v['id']." and p.id=o.st_id order by o.create_time desc";
           $zan_list__sql ="select p.user_name as user_name from tbl_opus_zan as o,tbl_people as p where o.opus_id=".$v['id']." and p.id=o.st_id order by o.create_time desc";
           $connection = Yii::app()->db;
           $command1 = $connection->createCommand($p_list_sql);
           $command2 = $connection->createCommand($comment_list__sql);
           $command3 = $connection->createCommand($zan_list__sql );
           $list1 = $command1->queryAll();
           $list2 = $command2->queryAll();
           $list3 = $command3->queryAll();
           $newlist[$k]['userinfo'] = People::model()->findByPk($v['st_id']);
           $newlist[$k]['opus'] = $v;
           $newlist[$k]['opuslist'] = $list1;
           $newlist[$k]['commentlist'] = $list2;
           $newlist[$k]['zanlist'] = $list3;
       }
        
        $this->render('opusList',
            array(
                'type'=>$type,
                'user_info'=>$user_info,
                'newlist'=>$newlist,
                'class_info'=>$class_info,
                'grade_info'=>$grade_info,
                'gid'=>$gid,
                'cid'=>$cid,
                'pname'=>$pname,
                'ptitle'=>$pTitle
            ));
    }
    
    public function actionMyLike(){
        $openid = Yii::app()->request->getParam('openid');
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $this->redirect($this->createUrl('index',array('openid'=>$openid)));
        }
        $user_info = People::model()->findByPk($user_id);//获取用户信息
        if($user_info->role!=1){//学生
            exit('error');
        }
        $list = Xihuan::model()->getLikeInfo($user_id);
        $this->pageTitle = '我喜爱的书籍';
        $this->render('my_like',array('openid'=>$openid,'list'=>$list));
    }
    public function actionMyStore(){
        $openid = Yii::app()->request->getParam('openid');
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $this->redirect($this->createUrl('index',array('openid'=>$openid)));
        }
        $user_info = People::model()->findByPk($user_id);//获取用户信息
        if($user_info->role!=1){//学生
            exit('error');
        }
        $list = Shoucang::model()->getLikeInfo($user_id);
        $this->pageTitle = '我收藏的书籍';
        $this->render('my_store',array('openid'=>$openid,'list'=>$list));
    }
    public function actionHot(){
        $openid = Yii::app()->request->getParam('openid');
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $this->redirect($this->createUrl('index',array('openid'=>$openid)));
        }
        //获取年级
        $grade_array = Grade::model()->findAll('is_show=:is_show and status=:status',array(':is_show'=>1,':status'=>1));
        $first = 0;
        $total = Yii::app()->params['listpage'];
        $list = Book::model()->getHotInfo($first,$total);
        $user_info = People::model()->findByPk($user_id);
        $this->render('hot',array('user_info'=>$user_info,'grade'=>$grade_array,'openid'=>$openid,'list'=>$list));
    }
    public function actionRead(){
        $openid = Yii::app()->request->getParam('openid');
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $this->redirect($this->createUrl('index',array('openid'=>$openid)));
        }
        //获取年级
        $grade_array = Grade::model()->findAll('is_show=:is_show and status=:status',array(':is_show'=>1,':status'=>1));
        $first = 0;
        $total = Yii::app()->params['listpage'];
        $list = Book::model()->getReadInfo($first,$total);
        $user_info = People::model()->findByPk($user_id);
        $this->render('read',array('user_info'=>$user_info,'grade'=>$grade_array,'openid'=>$openid,'list'=>$list));
    }



    public function actionAjaxBodyHot(){
        $this->layout = '//layouts/null';
        $cid = Yii::app()->request->getParam('cid');
        $gid = Yii::app()->request->getParam('gid');
        $title = Yii::app()->request->getParam('title');
        $openid = Yii::app()->request->getParam('openid');
        $first = 0;
        $total = Yii::app()->params['listpage'];
        $list = Book::model()->getHotInfo($first,$total,$gid,$cid,$title);
        $this->render('hot_ajax',array('list'=>$list,'openid'=>$openid));
    }
    public function actionAjaxBodyRead(){
        $this->layout = '//layouts/null';
        $cid = Yii::app()->request->getParam('cid');
        $gid = Yii::app()->request->getParam('gid');
        $openid = Yii::app()->request->getParam('openid');
        $first = 0;
        $total = Yii::app()->params['listpage'];
        $list = Book::model()->getReadInfo($first,$total,$gid,$cid);
        $this->render('read_ajax',array('list'=>$list,'openid'=>$openid));
    }
    public function actionAjaxHot(){
        $this->layout = '//layouts/null';
        $cid = Yii::app()->request->getParam('cid');
        $gid = Yii::app()->request->getParam('gid');
        $title = Yii::app()->request->getParam('title');
        $page = Yii::app()->request->getParam('page');
        $openid = Yii::app()->request->getParam('openid');
        $first = $page*Yii::app()->params['listpage'];
        $total = Yii::app()->params['listpage'];
        $list = Book::model()->getHotInfo($first,$total,$gid,$cid,$title);
        $this->render('hot_ajax',array('list'=>$list,'openid'=>$openid));
    }
    public function actionAjaxRead(){
        $this->layout = '//layouts/null';
        $cid = Yii::app()->request->getParam('cid');
        $gid = Yii::app()->request->getParam('gid');
        $openid = Yii::app()->request->getParam('openid');
        $page = Yii::app()->request->getParam('page');
        $first = $page*Yii::app()->params['listpage'];
        $total = Yii::app()->params['listpage'];
        $list = Book::model()->getReadInfo($first,$total,$gid,$cid);
        $this->render('read_ajax',array('list'=>$list,'openid'=>$openid));
    }
    public function actionLike(){
        $openid = Yii::app()->request->getParam('openid');
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $this->redirect($this->createUrl('index',array('openid'=>$openid)));
        }
        //获取年级
        $grade_array = Grade::model()->findAll('is_show=:is_show and status=:status',array(':is_show'=>1,':status'=>1));
        $first = 0;
        $total = Yii::app()->params['listpage'];
        $list = Book::model()->getLikeInfo($first,$total);
        $user_info = People::model()->findByPk($user_id);
        $this->render('like',array('user_info'=>$user_info,'grade'=>$grade_array,'openid'=>$openid,'list'=>$list));
    }
    public function actionStudent(){
        $openid = Yii::app()->request->getParam('openid');
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $this->redirect($this->createUrl('index',array('openid'=>$openid)));
        }
        $first = 0;
        $user_info = People::model()->findByPk($user_id);
        $total = Yii::app()->params['listpage'];
        $list = BookComment::model()->getStudentComment($user_info->cid,$first,$total,'',1,$user_id);
        $grade_array = Grade::model()->findAll('is_show=:is_show and status=:status',array(':is_show'=>1,':status'=>1));
        $this->render('student',array('openid'=>$openid,'list'=>$list,'grade'=>$grade_array));
    }
    public function actionAjaxStudent(){
        $this->layout = '//layouts/null';
        $openid = Yii::app()->request->getParam('openid');
        $title = Yii::app()->request->getParam('user_name');
        $page = Yii::app()->request->getParam('page');
        $user_id = Yii::app()->session['user_id'];
        $user_info = People::model()->findByPk($user_id);
        $first = $page*Yii::app()->params['listpage'];
        $total = Yii::app()->params['listpage'];
        $key = Yii::app()->request->getParam('key');
        if(!$key){
            $key = 1;
        }
        $cid = Yii::app()->request->getParam('cid');
        if(!$cid){
            $cid = $user_info->cid;
        }
        $list = BookComment::model()->getStudentComment($cid,$first,$total,$title,$key,$user_info->id);
        $this->render('ajax_student',array('openid'=>$openid,'list'=>$list));
    }
    public function actionAjaxBodyStudent(){
        $this->layout = '//layouts/null';
        $openid = Yii::app()->request->getParam('openid');
        $title = Yii::app()->request->getParam('user_name');
        $user_id = Yii::app()->session['user_id'];
        $user_info = People::model()->findByPk($user_id);
        $first = 0;
        $total = Yii::app()->params['listpage'];
        $cid = Yii::app()->request->getParam('cid');
        if(!$cid){
            $cid = $user_info->cid;
        }
        $list = BookComment::model()->getStudentComment($cid,$first,$total,$title,1,$user_id);
        $this->render('ajax_student',array('openid'=>$openid,'list'=>$list));
    }







    public function actionChild(){
        $openid = Yii::app()->request->getParam('openid');
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $this->redirect($this->createUrl('index',array('openid'=>$openid)));
        }
        $first = 0;
        $user_info = People::model()->findByPk($user_id);
        $total = Yii::app()->params['listpage'];
        $list = BookComment::model()->getChildComment($user_info->relation_id,$first,$total);
        $this->render('child',array('openid'=>$openid,'list'=>$list,'user_info'=>$user_info));
    }
    public function actionAjaxChild(){
        $this->layout = '//layouts/null';
        $openid = Yii::app()->request->getParam('openid');
        $page = Yii::app()->request->getParam('page');
        $user_id = Yii::app()->session['user_id'];
        $user_info = People::model()->findByPk($user_id);
        $first = $page*Yii::app()->params['listpage'];
        $total = Yii::app()->params['listpage'];
        $list = BookComment::model()->getChildComment($user_info->relation_id,$first,$total);
        $this->render('ajax_student',array('openid'=>$openid,'list'=>$list,'user_info'=>$user_info));
    }








    public function actionAjaxLike(){
        $this->layout = '//layouts/null';
        $cid = Yii::app()->request->getParam('cid');
        $gid = Yii::app()->request->getParam('gid');
        $openid = Yii::app()->request->getParam('openid');
        $page = Yii::app()->request->getParam('page');
        $first = $page*Yii::app()->params['listpage'];
        $total = Yii::app()->params['listpage'];
        $list = Book::model()->getLikeInfo($first,$total,$gid,$cid);
        $this->render('like_ajax',array('list'=>$list,'openid'=>$openid));
    }
    public function actionAjaxBodyLike(){
        $this->layout = '//layouts/null';
        $cid = Yii::app()->request->getParam('cid');
        $gid = Yii::app()->request->getParam('gid');
        $openid = Yii::app()->request->getParam('openid');
        $first = 0;
        $total = Yii::app()->params['listpage'];
        $list = Book::model()->getLikeInfo($first,$total,$gid,$cid);
        $this->render('like_ajax',array('list'=>$list,'openid'=>$openid));
    }
    public function actionMyShare(){
        $openid = Yii::app()->request->getParam('openid');
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $this->redirect($this->createUrl('index',array('openid'=>$openid)));
        }
        $user_info = People::model()->findByPk($user_id);//获取用户信息
        if($user_info->role!=1){//学生
            exit('error');
        }
        $list = BookComment::model()->getShare($user_id);
        $this->pageTitle = '我的心得';
        $this->render('my_share',array('openid'=>$openid,'list'=>$list));
    }
    public function actionMyPointlist(){
        $openid = Yii::app()->request->getParam('openid');
        $type = Yii::app()->request->getParam('type');
        $child_info = '';
        if(!$type){
            $type = 1;
        }
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $this->redirect($this->createUrl('index',array('openid'=>$openid)));
        }
        $user_info = People::model()->findByPk($user_id);//获取用户信息
        if($user_info->role==2){//家长的列表
            $child_info = People::model()->findByPk($user_info->relation_id);
            $list = People::model()->getListPoint($type,$child_info);
        }else{
            $list = People::model()->getListPoint($type,$user_info);
        }
        //获取用户排行
        if($user_info->role==1){
            $paihang = People::model()->getPaihang($type,$user_info);
        }elseif($user_info->role==2){
            $paihang = People::model()->getPaihang($type,$child_info);
        }else{
            $paihang = '-';
        }
        $this->pageTitle = '积分排行';
        $this->render('my_point_list',array('child_info'=>$child_info,'paihang'=>$paihang,'user_info'=>$user_info,'openid'=>$openid,'list'=>$list));
    }
    public function actionMyPointlist1(){
        $openid = Yii::app()->request->getParam('openid');
        $type = Yii::app()->request->getParam('type');
        $child_info = '';
        if(!$type){
            $type = 1;
        }
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $this->redirect($this->createUrl('index',array('openid'=>$openid)));
        }
        $user_info = People::model()->findByPk($user_id);//获取用户信息
        if($user_info->role==2){//家长的列表
            $child_info = People::model()->findByPk($user_info->relation_id);
            $list = People::model()->getListPoint($type,$child_info);
        }else{
            $list = People::model()->getListPoint($type,$user_info);
        }
        //获取用户排行
        if($user_info->role==1){
            $paihang = People::model()->getPaihang($type,$user_info);
        }elseif($user_info->role==2){
            $paihang = People::model()->getPaihang($type,$child_info);
        }else{
            $paihang = '-';
        }
        $this->pageTitle = '积分排行';
        $this->render('my_point_list1',array('child_info'=>$child_info,'paihang'=>$paihang,'user_info'=>$user_info,'openid'=>$openid,'list'=>$list));
    }

    public function actionStudentCount(){
        $openid = Yii::app()->request->getParam('openid');
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $this->redirect($this->createUrl('index',array('openid'=>$openid)));
        }
        $user_info = People::model()->findByPk($user_id);//获取用户信息
        $db = Yii::app()->db->createCommand();
        $haipan_sql = "SELECT t2.user_name,t2.code_num,t3.count,t2.gid,t2.cid,t4.title as gtitle,t5.title as ctitle FROM tbl_people t2 LEFT JOIN tbl_grade t4 ON t4.id=t2.gid LEFT JOIN tbl_classes t5 ON t5.id=t2.cid LEFT JOIN (SELECT COUNT(*) as count,t1.pid FROM tbl_book_comment t1 GROUP BY pid) t3 ON t3.pid=t2.id WHERE t2.role=1 ORDER BY t2.gid ASC,t2.cid ASC,t3.count DESC";
        $connection = Yii::app()->db;
        $command = $connection->createCommand($haipan_sql);
        $list = $command->queryAll();
        $this->pageTitle = '学生心得统计';
        $grade_array = Grade::model()->findAll('is_show=:is_show and status=:status',array(':is_show'=>1,':status'=>1));
        $this->render('student_count',array('user_info'=>$user_info,'openid'=>$openid,'list'=>$list,'grade'=>$grade_array));
    }
    public function actionAjaxStudentCount(){
        $openid = Yii::app()->request->getParam('openid');
        $gid = Yii::app()->request->getParam('gid');
        $cid = Yii::app()->request->getParam('cid');
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        $user_info = People::model()->findByPk($user_id);//获取用户信息
        $haipan_sql = "SELECT t2.user_name,t2.code_num,t3.count,t2.gid,t2.cid,t4.title as gtitle,t5.title as ctitle FROM tbl_people t2 LEFT JOIN tbl_grade t4 ON t4.id=t2.gid LEFT JOIN tbl_classes t5 ON t5.id=t2.cid LEFT JOIN (SELECT COUNT(*) as count,t1.pid FROM tbl_book_comment t1 GROUP BY pid) t3 ON t3.pid=t2.id WHERE t2.role=1  AND t2.cid = $cid  ORDER BY t2.gid ASC,t2.cid ASC,t3.count DESC";
        $connection = Yii::app()->db;
        $command = $connection->createCommand($haipan_sql);
        $list = $command->queryAll();
        $this->renderPartial('ajax_student_count',array('user_info'=>$user_info,'openid'=>$openid,'list'=>$list));
    }







    public function actionTeacherCount(){
        $openid = Yii::app()->request->getParam('openid');
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $this->redirect($this->createUrl('index',array('openid'=>$openid)));
        }
        $user_info = People::model()->findByPk($user_id);//获取用户信息
        $db = Yii::app()->db->createCommand();
        $haipan_sql = "SELECT t2.user_name,t2.code_num,t3.count,t2.gid,t2.cid,t4.title as gtitle,t5.title as ctitle FROM tbl_people t2 LEFT JOIN tbl_grade t4 ON t4.id=t2.gid LEFT JOIN tbl_classes t5 ON t5.id=t2.cid LEFT JOIN (SELECT COUNT(*) as count,t1.pid FROM tbl_book_comment t1 GROUP BY pid) t3 ON t3.pid=t2.id WHERE t2.role=3 ORDER BY t2.gid ASC,t2.cid ASC,t3.count DESC";
        $connection = Yii::app()->db;
        $command = $connection->createCommand($haipan_sql);
        $list = $command->queryAll();
        $this->pageTitle = '教师点评统计';
        $this->render('teacher_count',array('user_info'=>$user_info,'openid'=>$openid,'list'=>$list));
    }






    public function actionMyPoint(){
        $openid = Yii::app()->request->getParam('openid');
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $this->redirect($this->createUrl('index',array('openid'=>$openid)));
        }
        $user_info = People::model()->findByPk($user_id);//获取用户信息
        if($user_info->role!=1){//学生
            exit('error');
        }
//        $list = Point::model()->getMyPoint($user_id,0,6);
        //读书签到
        $dsDb = Yii::app()->db->createCommand("SELECT SUM(num) as count FROM {{point}} WHERE pid=$user_id AND title='读书签到'");
        $ds = $dsDb->queryRow();
        //教师评分
        $jsDb = Yii::app()->db->createCommand("SELECT SUM(num) as count FROM {{point}} WHERE pid=$user_id AND title='教师评分'");
        $js = $jsDb->queryRow();
        //心得分享
        $xdDb = Yii::app()->db->createCommand("SELECT SUM(num) as count FROM {{point}} WHERE pid=$user_id AND title='心得分享'");
        $xd = $xdDb->queryRow();
        //积分兑换DODO券
        $ddqDb = Yii::app()->db->createCommand("SELECT SUM(num) as count FROM {{point}} WHERE pid=$user_id AND title='积分兑换DODO券'");
        $ddq = $ddqDb->queryRow();
        //家长评价
        $jzDb = Yii::app()->db->createCommand("SELECT SUM(num) as count FROM {{point}} WHERE pid=$user_id AND title='家长评价'");
        $jz = $jzDb->queryRow();
        //同学评价
        $txDb = Yii::app()->db->createCommand("SELECT SUM(num) as count FROM {{point}} WHERE pid=$user_id AND title='同学评价'");
        $tx = $txDb->queryRow();
        $this->pageTitle = '我的积分';
//        $this->render('my_point',array(,'user_info'=>$user_info,'openid'=>$openid,'list'=>$list));
        $this->render('my_point',array('ds'=>$ds,'js'=>$js,'xd'=>$xd,'ddq'=>$ddq,'jz'=>$jz,'tx'=>$tx,'user_info'=>$user_info,'openid'=>$openid));
    }
    public function actionMyExchange(){
        $openid = Yii::app()->request->getParam('openid');
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $this->redirect($this->createUrl('index',array('openid'=>$openid)));
        }
        $user_info = People::model()->findByPk($user_id);//获取用户信息
        if($user_info->role!=1){//学生
            exit('error');
        }
        //获取最新一条积分数据

        $data = Gift::model()->getNewData();
        $this->pageTitle = '积分兑换';
        $this->render('my_exchange',array('user_info'=>$user_info,'openid'=>$openid,'data'=>$data));
    }
    public function actionMyQuan(){
        $openid = Yii::app()->request->getParam('openid');
        //1  session 记录
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $this->redirect($this->createUrl('index',array('openid'=>$openid)));
        }
        $user_info = People::model()->findByPk($user_id);//获取用户信息
        if($user_info->role!=1){//学生
            exit('error');
        }
//        $jiaDb = Yii::app()->db->createCommand("SELECT SUM(point) as count FROM {{quan}} WHERE type=1 AND pid=$user_id");
//        $jia = $jiaDb->queryRow();
//        $jianDb = Yii::app()->db->createCommand("SELECT SUM(point) as count FROM {{quan}} WHERE type=0 AND pid=$user_id");
//        $jian = $jianDb->queryRow();
//        $count = $jia['count'] - $jian['count'];
        $jiaDb = Yii::app()->db->createCommand("SELECT COUNT(*) as count FROM {{quan}} WHERE type=1 AND pid=$user_id");
        $jia = $jiaDb->queryRow();
        $jianDb = Yii::app()->db->createCommand("SELECT COUNT(*) as count FROM {{quan}} WHERE type=0 AND pid=$user_id");
        $jian = $jianDb->queryRow();
        $count = $jia['count'] - $jian['count'];
        $this->pageTitle = '我的DODO券';
        $this->render('my_quan',array('user_info'=>$user_info,'openid'=>$openid,'count'=>$count));
    }
    public function actionAjaxExchange(){
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $data['status'] = 0;
            $data['message'] = '获取用户ID失败';
            echo json_encode($data);
            exit();
        }
        $id = Yii::app()->request->getParam('id');
        $info = Gift::model()->findByPk($id);
        if(!$info){
            $data['status'] = 0;
            $data['message'] = '不存在此物品';
            echo json_encode($data);
            exit();
        }
        //--------存在 --------
        //1：积分检查
        $jiaDb = Yii::app()->db->createCommand("SELECT SUM(num) as count FROM {{point}} WHERE type=1 AND pid=$user_id");
        $jia = $jiaDb->queryRow();
        $jianDb = Yii::app()->db->createCommand("SELECT SUM(num) as count FROM {{point}} WHERE type=0 AND pid=$user_id");
        $jian = $jianDb->queryRow();
        $count = $jia['count'] - $jian['count'];
        if(($count - $info->point)<0){
            $data['status'] = 0;
            $data['message'] = '你的积分不够，不能兑换';
            echo json_encode($data);
            exit();
        }
        //-----积分足够 可以兑换-------
        $user_info = People::model()->findByPk($user_id);
        $point = $count - $info->point;
        //point 添加信息
        $pointDb = new Point();
        $pointDb->pid = $user_id;
        $pointDb->num = $info->point;
        $pointDb->type = 0;
        $pointDb->title = '积分兑换DODO券';
        $pointDb->create_time = time();
        if($pointDb->save()){
            //用户表减相应积分
            $user_info->point = $point;
            $user_info->update();
            //DODO券表添加信息
            $qDb = new Quan();
            $qDb->pid = $user_id;
            $qDb->gid = $info->id;
            $qDb->g_title =  $info->title;
            $qDb->point = $info->point;
            $qDb->type = 1;
            $qDb->create_time = time();
            if($qDb->save()){
                $data['status'] = 1;
                $data['message'] = '兑换成功';
                echo json_encode($data);
                exit();
            }else{
                $data['status'] = 0;
                $data['message'] = '兑换失败';
                echo json_encode($data);
                exit();
            }
        }else{
            $data['status'] = 0;
            $data['message'] = '兑换失败';
            echo json_encode($data);
            exit();
        }
    }

    public function actionAjaxPointList(){
        $this->layout = '//layouts/null';
        $page =  Yii::app()->request->getParam('page');
        $user_id = Yii::app()->session['user_id'];
        $first = $page*6;
        $total = 6;
        $list = Point::model()->getMyPoint($user_id,$first,$total);
        $this->render('my_point_ajax',array('list'=>$list));
    }
    public function actionAjaxCheckUser(){
        if($_POST['stroeUser']){
            $id = Xtutuquan::encryption($_POST['stroeUser'],1);
            if($id){
                $data['status'] = 1;
                Yii::app()->session['user_id'] = $id;
            }else{
                $data['status'] = 0;
            }
            echo json_encode($data);
            exit();
        }else{
            $data['status'] = 0;
            echo json_encode($data);
            exit();
        }
    }
    
    public function actionAjaxProductionZan(){
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $data['status'] = 0;
            $data['message'] = '获取用户ID失败';
            echo json_encode($data);
            exit();
        }
        $user_info = People::model()->findByPk($user_id);//获取用户信息
        if($user_info->role==1){
            $zan_from='S';
            $zan_num=1;
        }elseif($user_info->role==3){
            $zan_from='T';
            $zan_num=5;
        }
        $oid = Yii::app()->request->getParam('oid');
        $data['oid'] = $oid;
        $zanDb = Yii::app()->db->createCommand("SELECT * FROM tbl_opus_zan as o WHERE o.opus_id=$oid and o.st_id=$user_id");
        $zan = $zanDb->queryRow();
        $data['zan'] = $zan;
        if($zan['id']>0){
            $data['status'] = 0;
            $data['message'] = '你已点过赞';
            echo json_encode($data);
            exit();
        }else{
            try {
                $zanModel = Yii::app()->db->createCommand("INSERT INTO tbl_opus_zan(opus_id,st_id,zan_from,create_time) values($oid,$user_id,'$zan_from',NOW())")->execute();
                $opusModel = Yii::app()->db->createCommand("update tbl_opus as o set o.total_zan=o.total_zan+$zan_num  where o.id=$oid")->execute();
                $data['status'] = 1;
                $data['message'] = '点赞成功';
                echo json_encode($data);
                exit();
            } catch (Exception $e) {
                $data['status'] = 0;
                $data['message'] = '点赞失败';
                $data['catch'] = $e;
                echo json_encode($data);
                exit();
            }
             }
    }
    public function actionAjaxProductionPl(){
     $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $data['status'] = 0;
            $data['message'] = '获取用户ID失败';
            echo json_encode($data);
            exit();
        }
        $user_info = People::model()->findByPk($user_id);//获取用户信息
        if($user_info->role==1){
            $zan_from='S';
            $zan_num=1;
        }elseif($user_info->role==3){
            $zan_from='T';
            $zan_num=5;
        }
        $oid = Yii::app()->request->getParam('oid');
        $cotent = Yii::app()->request->getParam('content');
            try {
                $plModel = Yii::app()->db->createCommand("INSERT INTO tbl_opus_comment(content,opus_id,st_id,comment_from,create_time) values('$cotent',$oid,$user_id,'$zan_from',NOW())")->execute();
                 $data['status'] = 1;
                $data['message'] = '评论成功';
                echo json_encode($data);
            } catch (Exception $e) {
                $data['status'] = 0;
                $data['message'] = '评论失败';
                $data['catch'] = $e;
                echo json_encode($data);
                exit();
            }
    }
    
    public function actionAjaxProductionDel(){
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $data['status'] = 0;
            $data['message'] = '获取用户ID失败';
            echo json_encode($data);
            exit();
        }
        $user_info = People::model()->findByPk($user_id);//获取用户信息
        if($user_info->role==1){
            $zan_from='S';
            $zan_num=1;
        }elseif($user_info->role==3){
            $zan_from='T';
            $zan_num=5;
        }
        $oid = Yii::app()->request->getParam('oid');
     try {
                $plModel = Yii::app()->db->createCommand("DELETE FROM tbl_opus where id=$oid")->execute();
                 $data['status'] = 1;
                $data['message'] = "删除成功";
                echo json_encode($data);
                exit();
            } catch (Exception $e) {
                $data['status'] = 0;
                $data['message'] = "删除失败";
                $data['e'] = $e;
                echo json_encode($data);
                exit();
            }
    }
    
    public function actionCreate()
    {
        $model=new Product;
        if(isset($_POST['Product']))
        {
            $model->attributes=$_POST['Product'];
            if($model->save()){
                if($_FILES['imageFile']['name']!=null)
                {
                    $model->setImageInformation($_FILES);
                }
                
            }
        }
        $this->render('create',array(
            'model'=>$model,
        ));
    }
    
    public function actionDeleteIma()
    {
        $ima_id=$_POST['ima_id'];
        $image_info=Proimg::model()->findByPk($ima_id);
    
        $img_url= "..".Yii::app()->request->baseUrl.$image_info['url'];
        if(file_exists($img_url)){
            unlink($img_url);
        }
        $a=$image_info->delete();
        echo $a;
    }
    
    public function actionUpload()
    {
        $user_id = Yii::app()->session['user_id'];
        if(!$user_id){
            $data['status'] = 0;
            $data['message'] = '获取用户ID失败';
            echo json_encode($data);
            exit();
        }
        $title = Yii::app()->request->getParam('title');
        $desc = Yii::app()->request->getParam('desc');
        
        $dir = date('Ym',time());
        $image = CUploadedFile::getInstanceByName('file0');
        $dirs='uploads/'.$dir.'/';
        $dir=Yii::getPathOfAlias('webroot').'/uploads/'.$dir.'/';
        
        //上传目录
        if (!is_dir($dir)) {
            mkdir( $dir,0777,true);
            //目录不存在则创建
        }
        $name = $dir.strtotime("now").$image->name;
        $names = $dirs.strtotime("now").$image->name;
        //文件名绝对路径
        
        $status = $image->saveAs(iconv('utf-8','gb2312',$name),true);
        //保存文件
        if ($status) {
            $opusModel = Yii::app()->db->createCommand("INSERT INTO tbl_opus(opus_title,opus_desc,opus_type,st_id,opus_from,opus_source_type,total_zan,create_time) values('$title','$desc',1,$user_id,'S','作品上传',0,NOW())")->execute();
            $id = Yii::app()->db->getLastInsertID();
            $opuslistModel = Yii::app()->db->createCommand("INSERT INTO tbl_opus_list(opus_id,opus_path) values($id,'$names')")->execute();
            $data['status'] = 1;
                $data['message'] = '上传成功';
                 $control=Yii::app()->runController('user/opuslist/type/2');
        }else {
            $data['status'] = 0;
                $data['message'] = '上传失败';
                 $control=Yii::app()->runController('user/opuslist/type/2');
        }
    }
}